package com.example.eshop_ssm.controller;

import com.example.eshop_ssm.entity.TbTypeInfo;
import com.example.eshop_ssm.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/type")
public class TypeManagerController {

    @Autowired
    private TypeService typeService;
    /**
     * 转发在分类页面列表页面
     * @return
     * @throws Exception
     */
    @RequestMapping("/typeView")
    public ModelAndView typeView()throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        List<TbTypeInfo> typeList= typeService.typeSelect();

        for (TbTypeInfo typeListS:typeList
             ) {
            System.out.println(typeListS);
        }
         //将数据转发到页面
        modelAndView.addObject("typeList",typeList);
        modelAndView.setViewName("typemanager");

        return modelAndView;
    }


    /**
     * 添加一级分类
     * @param type_name
     * @param parent_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/TypeInst")
    @ResponseBody
    public Map<String,Object>TypeInst(
            @RequestParam(value = "type_name") String type_name,
            @RequestParam("parent_id") Integer parent_id
    )throws Exception{
        boolean isok=typeService.addTypeOne(type_name,parent_id);

        Map<String,Object> map=new HashMap<>();
        map.put("isok",isok);
       return map;
    }

    /**
     * 查询一级分类列表
     * @return
     * @throws Exception
     */
    @RequestMapping("/TypeSelectOne")
    @ResponseBody
    public Map<String,Object> TypeSelectOne()throws Exception{
        List<TbTypeInfo>  listOne=typeService.selectOne();

        Map<String,Object> map=new HashMap<>();
        map.put("listOne",listOne);
        return map;
    }

    /**
     * 删除一级分类联动数据
     * @param type_id
     * @return
     * @throws Exception
     */
    @RequestMapping("/deleteOne")
    @ResponseBody
    public  Map<String,Object> deleteOne(
           @RequestParam("type_id") Integer type_id
    )throws Exception{

        boolean isok=typeService.deleteTypeOne(type_id);

        Map<String,Object> map=new HashMap<>();
        map.put("isok",isok);
        return map;
    }

}
